Skip to content

harden ember-persistence — safe u32 length casts in format layer - #104

Merged
kacy merged 1 commit into
mainfrom
harden/ember-persistence
Feb 12, 2026
Merged

harden ember-persistence — safe u32 length casts in format layer#104
kacy merged 1 commit into
mainfrom
harden/ember-persistence

Conversation

@kacy

@kacy kacy commented Feb 12, 2026

Copy link
Copy Markdown
Owner

summary

replaces all bare len() as u32 casts in the persistence format layer with checked conversions that return errors instead of silently truncating.

  • adds format::write_len() helper using u32::try_from()
  • updates 15 call sites across AOF and snapshot writers
  • guards format::write_bytes() the same way
  • guards encrypted ciphertext length in snapshot writer

while collections exceeding u32::MAX items is unrealistic for in-memory data, this eliminates the class of silent data corruption that would occur if it ever happened. the check is essentially free in practice.

what was tested

  • all 67 ember-persistence unit tests pass
  • round-trip tests for all data types (strings, lists, sorted sets, hashes, sets)
  • corruption detection tests still work
  • cargo clippy -p ember-persistence clean

replace bare `len() as u32` casts with checked conversions:

- add format::write_len() helper that uses u32::try_from() and returns
  an error if the length exceeds u32::MAX
- update all collection length writes in aof.rs and snapshot.rs to use
  the safe helper (10 call sites in AOF, 5 in snapshot)
- guard encrypted ciphertext length with try_from in snapshot writer
- also guard format::write_bytes() the same way

while >4B items is unrealistic for in-memory collections, this prevents
silent data corruption if it ever happened, and the error path is
essentially free since the check always succeeds in practice.
@kacy
kacy merged commit aa577b6 into main Feb 12, 2026
7 checks passed
@kacy
kacy deleted the harden/ember-persistence branch February 12, 2026 16:34
kacy added a commit that referenced this pull request Feb 19, 2026
replace bare `len() as u32` casts with checked conversions:

- add format::write_len() helper that uses u32::try_from() and returns
  an error if the length exceeds u32::MAX
- update all collection length writes in aof.rs and snapshot.rs to use
  the safe helper (10 call sites in AOF, 5 in snapshot)
- guard encrypted ciphertext length with try_from in snapshot writer
- also guard format::write_bytes() the same way

while >4B items is unrealistic for in-memory collections, this prevents
silent data corruption if it ever happened, and the error path is
essentially free since the check always succeeds in practice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant